﻿namespace SVNWrapper
{
    using System;
    using System.Diagnostics;
    using System.IO;

    public class Logger
    {
        private static Logger _LoggerInstance = null;
        public static string DebuggerLogFilePath = "DebuggerFileLog.deb";
        public static string DefaultLogFilePath = @"c:\temp\CSharpLog.txt";

        private Logger()
        {
        }

        public void DebugLog(string category, string location, string msg)
        {
            if (Debugger.IsAttached)
            {
                string str = string.Format("[{0}] {1} -> {2}", category, location, msg);
                this.Log(str, DebuggerLogFilePath);
            }
        }

        public void Log(string msg)
        {
            this.Log(msg, DefaultLogFilePath);
        }

        public void Log(string msg, string logFilePath)
        {
            try
            {
                if (!File.Exists(logFilePath))
                {
                    try
                    {
                        File.CreateText(logFilePath).Close();
                    }
                    catch
                    {
                    }
                }
                StreamReader reader = new StreamReader(logFilePath);
                string str = reader.ReadToEnd();
                reader.Close();
                StreamWriter writer2 = new StreamWriter(logFilePath);
                writer2.Write(str);
                writer2.WriteLine("[{0}] - {1}", DateTime.Now.ToShortDateString() + DateTime.Now.ToLongTimeString(), msg);
                writer2.Close();
            }
            catch
            {
            }
        }

        public static Logger LoggerInstance
        {
            get
            {
                return ((_LoggerInstance == null) ? (_LoggerInstance = new Logger()) : _LoggerInstance);
            }
        }
    }
}

